package com.itlxd.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itlxd.dto.PageDTO;
import com.itlxd.dto.SetmealDTO;
import com.itlxd.entity.DishFlavor;
import com.itlxd.entity.Setmeal;
import com.itlxd.entity.SetmealDish;
import com.itlxd.exception.ArgsException;
import com.itlxd.exception.SqlOperationException;
import com.itlxd.global.ErrorCode;
import com.itlxd.global.SysConstants;
import com.itlxd.global.SysMessages;
import com.itlxd.result.ResultVO;
import com.itlxd.service.IDishFlavorService;
import com.itlxd.service.IDishService;
import com.itlxd.service.ISetmealDishService;
import com.itlxd.service.ISetmealService;
import com.itlxd.vo.DishVO;
import com.itlxd.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 * @author ${author}
 * @since 2021-12-25
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetmealDishService setmealDishService;

    /**
     * 分页查询套餐
     * @param pageDTO
     * @return
     */
    @GetMapping("/page")
    public ResultVO<IPage<SetmealVO>> listSetmealsByPage(PageDTO pageDTO) {
        IPage<SetmealVO> setmealIPage = setmealService.listPageSetmeals(pageDTO);

        return ResultVO.success(setmealIPage,SysMessages.QUERY_SUCCESS);
    }

    /**
     * 添加套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    public ResultVO<Integer> addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        if (null == setmealDTO) {
            throw new ArgsException(SysMessages.ARGS_IS_NULL);
        }

        Integer insertRows = setmealService.insertSetmeal(setmealDTO);
        if (null == insertRows || insertRows < SysConstants.NUM_ZERO) {
            throw new SqlOperationException(ErrorCode.INSERT_DEFAULT_ERROR, SysMessages.INSERT_FAIL);
        }

        return ResultVO.success(SysMessages.INSERT_SUCCESS);
    }

    /**
     * 通过id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultVO<SetmealVO> querySetmealById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getById(id);
        if (null == setmealVO) {
            throw new SqlOperationException(ErrorCode.QUERY_DEFAULT_ERROR, SysMessages.QUERY_FAIL);
        }
        return ResultVO.success(setmealVO, SysMessages.QUERY_SUCCESS);
    }

    /**
     * 更新套餐（包括套餐下的菜品）
     * @param setmealDTO
     * @return
     */
    @PutMapping
    public ResultVO<Integer> updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        Integer updateRows = setmealService.updateSetmeal(setmealDTO);
        if (null == updateRows) {
            throw new ArgsException(ErrorCode.UPDATE_DEFAULT_ERROR, SysMessages.UPDATE_FAIL);
        }
        return ResultVO.success(updateRows, SysMessages.UPDATE_SUCCESS);
    }

    /**
     * 批量删除套餐（包括套餐下的菜品）
     * @param ids
     * @return
     */
    @DeleteMapping
    public ResultVO<Integer> deleteBatchSetmealWithSetmealDishes(Long[] ids) {
        Integer delRows = setmealService.deleteBatchSetmeals(ids);

        if (null == delRows || delRows < SysConstants.NUM_ZERO) {
            throw new SqlOperationException(ErrorCode.DELETE_DEFAULT_ERROR, SysMessages.DELETE_FAIL);
        }

        return ResultVO.success(delRows, SysMessages.DELETE_SUCCESS);
    }

    /**
     * 批量修改状态
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public ResultVO<Integer> switchSetmealStatus(@RequestParam Long[] ids, @PathVariable Integer status) {
        if (null == ids || ids.length <= SysConstants.NUM_ZERO || null == status) {
            throw new ArgsException(SysConstants.ARGS_IS_NULL, SysMessages.ARGS_IS_NULL);
        }

        Integer updateRows = setmealService.updateBatchStatusSetmeals(ids, status);

        if(null == updateRows) {
            throw new ArgsException(ErrorCode.UPDATE_DEFAULT_ERROR, SysMessages.UPDATE_FAIL);
        }

        return ResultVO.success(updateRows, SysMessages.UPDATE_SUCCESS);
    }

    /**
     * 查询套餐列表
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<Setmeal>> listSetmeals(Setmeal setmeal) {
        List<Setmeal> setmeals = setmealService.listSetmeals(setmeal);
        return ResultVO.success(setmeals, SysMessages.QUERY_SUCCESS);
    }


    /**
     * 根据id查询所有套餐
     * @param dishId
     * @return
     */
    @GetMapping("/dish/{dishId}")
    public ResultVO<List<DishVO>> listSetmealDishBySetmealId(@PathVariable Long dishId) {
        List<DishVO> dishVOList = dishService.listSetmealDishesByDishId(dishId);
        return ResultVO.success(dishVOList, SysMessages.QUERY_SUCCESS);
    }
}

